#version 140

in vec3 	in_Vertex;
in vec3 	in_Color;
in vec3		in_Normal;
in vec3		in_Texture;

uniform mat4	UniMatModelview;
uniform mat4	UniMatProjection;
uniform mat3	UniMatNormal;

uniform vec3	UniLightPosition;
uniform vec3	UniEyePosition;

uniform vec3	UniColorDiffuse;
uniform vec3	UniColorAmbient;
uniform vec3	UniColorSpecular;

smooth out vec3	out_Color;


vec3	ADSGouraudIllumination(vec3 lightPos, vec3 vertexPosition, vec3 vertexNormal, vec3 vertexColor)
{
	vec3	color;
	vec3	vecLight = normalize(lightPos - vertexPosition);
	float	coefDiffuse = max(0.0, dot(vertexNormal, vecLight));
	vec3	vecReflected = normalize(reflect(-vecLight, vertexNormal));
	float	coefSpec = max(0.0, dot(vertexNormal, vecReflected));
	
	color = UniColorDiffuse * coefDiffuse;
	color += UniColorAmbient;
	if (abs(coefSpec) > 0.005)
	{
		coefSpec = pow(coefSpec, 128.0);
		color += vec3(coefSpec);
	}
	
	return color;
}

void	main()
{
	mat3	matNormal = transpose(inverse(UniMatNormal));
	vec3	vertexNormal = UniMatNormal * in_Normal;

	vec4	vertexPos4 = UniMatProjection * UniMatModelview * vec4(in_Vertex, 1.0);
	vec3	vertexPos = vertexPos4.xyz / vertexPos4.w;

	vec4	lightPos4 = UniMatModelview * vec4(UniLightPosition, 1.0);
	vec3	lightPos = lightPos4.xyz / lightPos4.w;


	out_Color = ADSGouraudIllumination(lightPos, vertexPos, vertexNormal, vec3(1.0, 1.0, 1.0));
	gl_Position = UniMatProjection * UniMatModelview * vec4(in_Vertex, 1.0);
}